using System;
using System.Collections.Generic;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

using Migration.Classes;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Modules.Blogs;
using Telerik.Sitefinity.Modules.Events;
using Telerik.Sitefinity.Modules.GenericContent;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Modules.Lists;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.Modules.Pages.Web.UI;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Web.UI;

namespace Migration.PublicControls
{
	[RequireScriptManager]
	public class BackendOverviewControl : SimpleView
	{
		private string layoutTemplateName = "Migration.Resources.Views.BackendOverviewTemplate.ascx";
		private List<string> m_ImportCheckBoxes = new List<string>();

		protected override string LayoutTemplateName
		{
			get
			{
				return this.layoutTemplateName;
			}
		}

		protected virtual TextBox SiteUrl
		{
			get
			{
				return base.Container.GetControl<TextBox>("SiteUrl", true);
			}
		}

		protected virtual Label ReportLabel
		{
			get
			{
				return base.Container.GetControl<Label>("ReportLabel", true);
			}
		}

		protected virtual System.Web.UI.WebControls.Literal ProgressBackground
		{
			get
			{
				return base.Container.GetControl<System.Web.UI.WebControls.Literal>("ProgressBackground", true);
			}
		}

		protected virtual System.Web.UI.WebControls.Literal MigrationProgressIndicatorPreload
		{
			get
			{
				return base.Container.GetControl<System.Web.UI.WebControls.Literal>("MigrationProgressIndicatorPreload", true);
			}
		}

		protected System.Web.UI.HtmlControls.HtmlImage MigrationProgressIndicator
		{
			get
			{
				return base.Container.GetControl<System.Web.UI.HtmlControls.HtmlImage>("MigrationProgressIndicator", true);
			}
		}

		protected virtual DropDownList ProviderDropDown(string name)
		{
			return base.Container.GetControl<DropDownList>(name, true);
		}

		protected virtual Label CountLabel(string name)
		{
			return base.Container.GetControl<Label>(name, true);
		}

		private void FillDropDown<T>(IEnumerable<T> providers, string dropDownName) where T : IDataProviderBase
		{
			if (providers != null)
			{
				foreach (T provider in providers)
				{
					if (provider.Name != "AppRoles")
						ProviderDropDown(dropDownName).Items.Add(provider.Name);
				}
			}
		}

		private void FillProviderDropDowns()
		{
			FillDropDown(RoleManager.ProvidersCollection, "Providers_Roles");
			FillDropDown(UserManager.ProvidersCollection, "Providers_Users");
			FillDropDown(LibrariesManager.GetManager().Providers, "Providers_Libraries");
			FillDropDown(ContentManager.GetManager().Providers, "Providers_GenericContent");
			FillDropDown(NewsManager.GetManager().Providers, "Providers_News");
			FillDropDown(BlogsManager.GetManager().Providers, "Providers_Blogs");
			FillDropDown(EventsManager.GetManager().Providers, "Providers_Events");
			FillDropDown(ListsManager.GetManager().Providers, "Providers_Lists");
		}

		protected override void InitializeControls(GenericContainer controlContainer)
		{
			FillProviderDropDowns();

			if (Environment.MachineName == "VELKOV")
			{
				this.SiteUrl.Text = "http://localhost/telerik/TIUSite_v3.7_SP4";
			}

			ProgressBackground.Text = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Migration.Resources.ccc.gif");
			MigrationProgressIndicatorPreload.Text = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Migration.Resources.ccc.gif");
			MigrationProgressIndicator.Src = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Migration.Resources.ajax-loader.gif");

			// Update the web.config setting for the MigrationService
			string webConfigPath = HttpContext.Current.Request.ApplicationPath.ToString();
			Configuration conf = WebConfigurationManager.OpenWebConfiguration(webConfigPath);

			/*
			BehaviorsSection behaviorsSection = (BehaviorsSection)conf.GetSection("system.serviceModel/behaviors/endpointBehaviors");

			EndpointBehaviorElement endpointMigrationServiceBehavior = new EndpointBehaviorElement("EndpointMigrationServiceBehavior");
			behaviorsSection.EndpointBehaviors.Add(endpointMigrationServiceBehavior);

			ServiceBehaviorElement migrationServiceBehavior = new ServiceBehaviorElement("MigrationServiceBehavior");
			behaviorsSection.ServiceBehaviors.Add(migrationServiceBehavior);
			*/

			// Update the web.config setting for the JSON serialization
			ScriptingJsonSerializationSection section = (ScriptingJsonSerializationSection)conf.GetSection("system.web.extensions/scripting/webServices/jsonSerialization");
			section.MaxJsonLength = 50000000;
			conf.Save();

			ScriptManager sm = ScriptManager.GetCurrent(this.Page);
			sm.AsyncPostBackTimeout = 3600;

			ScriptReference jqueryReference1 = new ScriptReference("Telerik.Sitefinity.Resources.Scripts.jquery-1.5.1.min.js", "Telerik.Sitefinity.Resources");
			sm.Scripts.Add(jqueryReference1);

			ScriptReference jqueryReference2 = new ScriptReference("Telerik.Sitefinity.Resources.Scripts.jquery-1.5.2.min.js", "Telerik.Sitefinity.Resources");
			sm.Scripts.Add(jqueryReference2);

			MigrationSettings.ModulePage = this.Page;
		}		
	}
}
